import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//对于在确定时间里，开启任务，在其他时间里休眠任务，这个程序在模拟这个过程。
public class runStop {
    static Map<Thread,Object> ts = new HashMap();
    static volatile boolean isWait = false;
    static int nums=10;


    public static void suspendTask(){
        System.out.println("暂停所有任务执行...");
        isWait = true;
        outputState();
    }

    public static void main(String[] args) {
        startTask();
        int turns=0;
        while (turns<nums) {
            turns++;
            System.out.println("这是第"+turns+"轮处理");
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            suspendTask();
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            reStartTask();
       }
       terminateThread();
    }
    public static void reStartTask(){
        System.out.println("重启所有任务...");
        isWait = false;
        for(Map.Entry<Thread,Object> entry:ts.entrySet()){
            Object obj = entry.getValue();
            synchronized (obj){
                obj.notify();
            }
        }
    }

    public static void startTask(){
        System.out.println("所有任务运行中...");
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for(int i=0;i<5;i++){
            Object obj = new Object();
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    double d=0;
                    for(long i=0;i<Long.MAX_VALUE;i++){
                        if(isWait){
                            synchronized (obj){
                                try{
                                    System.out.println(Thread.currentThread().getId()+"休眠，i="+i);
                                    ts.put(Thread.currentThread(),obj);
                                    obj.wait();
                                    System.out.println(Thread.currentThread().getId()+"重启,i="+i);

                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        }
                        d += (Math.PI+Math.E)/(double) i;
                    }
                }
            });
        }
        pool.shutdownNow();
    }

    private static void outputState(){
        try{
            Thread.sleep(1000);
        }catch(Exception e){

        }
        for(Map.Entry<Thread,Object> entry: ts.entrySet()){
            System.out.println(entry.getKey().getId()+"状态："+entry.getKey().getState());
        }
    }

    private static void terminateThread(){
        try {
            for(Map.Entry<Thread,Object> entry: ts.entrySet()){
                System.out.println(entry.getKey().getId()+"状态："+entry.getKey().getState()+"开始关闭");
                entry.getKey().stop();
                System.out.println(entry.getKey().getId()+"状态："+entry.getKey().getState()+"完成");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
